var http = require('http');
var server = http.createServer();
var qure=require('querystring');
var logincode={"code":1, msg: "ok"};
var mysql=require('mysql');
var db_config={
    host:'localhost',
    user:'root',
    password:'ykc578103878',
    port:'3306',
    database:'user'
};
var Name,Grade;
var connect=mysql.createConnection(db_config);
connect.connect();
     server.on('request',function (req,res) {
            var data = '';
            req.on('data', function (che) {
                data += che;
            });
            req.on('end', function () {
                var data_obj=qure.parse(data);
                var dataname=data_obj.name;
                var datapsd=data_obj.password;
                //连接数据库
                //查找
                connect.query("SELECT * FROM userdatas WHERE userZ=? AND userP=?",[dataname,datapsd],function (err,result) {
                    console.log(result);
                    if (result.length==0){
                        logincode={"code":2, msg: "ok"};
                    }else {
                        Name=result[0]["userName"];
                        Grade=result[0]["userGrade"];
                        logincode={"code":1, msg: "ok","Name":Name,"Grade":Grade};
                    }
                    //connect.end();
                    console.log(logincode);
                    res.end(JSON.stringify(logincode));
                });
            });
     });
server.listen(666);